package net.oschina.simpleplus.sample.consumer;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import net.oschina.simpleplus.ellieapi.ClassHelper;
import net.oschina.simpleplus.ellieconsumer.FastMethodInvoker;
import net.oschina.simpleplus.ellieconsumer.NettyConsumer;
import net.sf.cglib.reflect.FastClass;

/**
 * Hello world!
 *
 */
public class SampleConsumer 
{
	

    public static void main( String[] args )
    {
    	ApplicationContext ac = new AnnotationConfigApplicationContext("net.oschina.simpleplus.sample.consumer");
    	Map<String, Method> scanForMethods = ClassHelper.scanForMethods("net.oschina.simpleplus.sample.consumer");
    	
    	Map<String, FastMethodInvoker> methodCaches = new HashMap<>();
    	scanForMethods.entrySet().forEach(e ->{
    		Object bean = ac.getBean(e.getValue().getDeclaringClass());
    		methodCaches.put(e.getKey(), new FastMethodInvoker(FastClass.create(e.getValue().getDeclaringClass()).getMethod(e.getValue()), bean));
    	});
    	NettyConsumer nettyConsumer = null;
    	String zooKeeperCluster = "127.0.0.1:2181";
    	try {
			nettyConsumer = new NettyConsumer(8899, methodCaches, zooKeeperCluster);
			nettyConsumer.block();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally {
			if(nettyConsumer != null)
				nettyConsumer.shutdown();
		}
    	
    			
    }
}
